package com.example.manpower.Server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.manpower.Dto.DepartmentStats;
import com.example.manpower.Entity.Department;
import com.example.manpower.Server.DepartmentService;
import com.example.manpower.Mapper.DepartmentMapper;
import com.example.manpower.Util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
* @author asus
* @description 针对表【department】的数据库操作Service实现
* @createDate 2024-10-11 09:53:56
*/
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService{
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public Result select(DepartmentStats departmentStats) {
        List<Department> num1 = departmentMapper.selectList(null);
        int total =num1.size();
        int start = (departmentStats.getPage()-1)*departmentStats.getSize();
        departmentStats.setStart(start);
        List<DepartmentStats> select = departmentMapper.select(departmentStats);
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("list",select);
        hashMap.put("total",total);
        return new Result(200,"查询成功",hashMap);
    }

    //新增部门
    @Override
    public Result add(Department department) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("department_name",department.getDepartmentName());
        if(departmentMapper.selectOne(queryWrapper)!=null){
            return new Result(400,"部门已存在",null);
        }
        int num = departmentMapper.insert(department);
        return new Result(200,"添加部门成功",num);
    }

    @Override
    public Result delete(Department department) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("department_id",department.getDepartmentId());
        int num = departmentMapper.delete(queryWrapper);
        return new Result(200,"删除部门成功",num);
    }

    @Override
    public Result updateOne(Department department) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("department_id",department.getDepartmentId());
        int num = departmentMapper.update(department,queryWrapper);
        return new Result(200,"修改部门成功",num);
    }

    @Override
    public Result selectall(Department department) {
        List<Department> departments = departmentMapper.selectList(null);
        System.out.println(departments);
        return new Result(200,"查询成功",departments);
    }
}




